<?php
/**
 * Class name: RSS
 */
namespace App\Service;

class Rss
{
    protected $rss_ver             = "2.0";
    protected $channel_title       = '';
    protected $channel_link        = '';
    protected $channel_description = '';
    protected $language            = 'zh_CN';
    protected $copyright           = '';
    protected $webMaster           = '';
    protected $pubDate             = '';
    protected $lastBuildDate       = '';
    protected $generator           = 'CTS RSS Generator';
    protected $content             = '';
    protected $items               = array();
    /**************************************************************************/
    // 函数名: RSS
    // 功能: 构造函数
    // 参数: $title
    // $link
    // $description
    /**************************************************************************/
    public function __construct($title, $link, $description)
    {
        $this->channel_title       = $title;
        $this->channel_link        = $link;
        $this->channel_description = $description;
        $this->pubDate             = Date('Y-m-d H:i:s', time());
        $this->lastBuildDate       = Date('Y-m-d H:i:s', time());
    }
    /**************************************************************************/
    // 函数名: AddItem
    // 功能: 添加一个节点
    // 参数: $title
    // $link
    // $description $pubDate
    /**************************************************************************/
    public function AddItem($title, $link, $description, $pubDate, $thumb)
    {
        $this->items[] = array(
            'title'       => $title,
            'link'        => $link,
            'description' => $description,
            'pubDate'     => $pubDate,
            'thumb'       => $thumb,
        );
    }
    /**************************************************************************/
    // 函数名: BuildRSS
    // 功能: 生成rss xml文件内容
    /**************************************************************************/
    public function BuildRSS()
    {
        $s = "<?xml version='1.0' encoding='utf-8'?>
	   \n<rss xmlns:atom='http://www.w3.org/2005/Atom' version='2.0'>\n";
        // start channel
        $s .= "<channel>\n";
        $s .= "<title><![CDATA[{$this->channel_title}]]></title>\n";
        $s .= "<link><![CDATA[{$this->channel_link}]]></link>\n";
        $s .= "<description><![CDATA[{$this->channel_description}]]></description>\n";
        $s .= "<language>{$this->language}</language>\n";
        if(!empty($this->copyright)){
            $s .= "<copyright><![CDATA[{$this->copyright}]]></copyright>\n";
        }
        if(!empty($this->webMaster)){
            $s .= "<webMaster><![CDATA[{$this->webMaster}]]></webMaster>\n";
        }
        if(!empty($this->pubDate)){
            $s .= "<pubDate>{$this->pubDate}</pubDate>\n";
        }
        if(!empty($this->lastBuildDate)){
            $s .= "<lastBuildDate>{$this->lastBuildDate}</lastBuildDate>\n";
        }
        if(!empty($this->generator)){
            $s .= "<generator>{$this->generator}</generator>\n";
        }
        // start items
        for($i = 0; $i < count($this->items); $i++){
            $s .= "<item>\n";
            $s .= "<title><![CDATA[{$this->items[$i]['title']}]]></title>\n";
            $s .= "<description><![CDATA[{$this->items[$i]['description']}]]></description>\n";
            $s .= "<link><![CDATA[{$this->items[$i]['link']}]]></link>\n";
            $s .= "<pubDate>{$this->items[$i]['pubDate']}</pubDate>\n";
            $s .= "<thumb>{$this->items[$i]['thumb']}</thumb>\n";
            $s .= "</item>\n";
        }
        // close channel
        $s             .= "</channel>\n</rss>";
        $this->content = $s;
    }
    /**************************************************************************/
    // 函数名: Show
    // 功能: 将产生的rss内容直接打印输出
    /**************************************************************************/
    public function Show()
    {
        if(empty($this->content)){
            $this->BuildRSS();
        }

        return $this->content;
    }
    /**************************************************************************/
    // 函数名: SaveToFile
    // 功能: 将产生的rss内容保存到文件
    // 参数: $fname 要保存的文件名
    /**************************************************************************/
    public function SaveToFile($fname)
    {
        $handle = fopen($fname, 'wb');
        if($handle === false) return false;
        fwrite($handle, $this->content);
        fclose($handle);

        return true;
    }

    public function getFile($fname)
    {
        $handle = fopen($fname, 'r');
        if($handle === false) return false;
        while(!feof($handle)){
            $result = fgets($handle);
        }
        fclose($handle);

        return $result;
    }

}

?>
